package com.example.demo.tool;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUserTool {

	private Connection connection;
	
	private Statement statement;

	private String createUserSQL = "CREATE USER \"user_name\" LOGIN PASSWORD 'password';";

	private String createSuperUserSQL = "CREATE USER \"user_name\" WITH SYSADMIN PASSWORD 'password';";

	private String dropUserSQL = "DROP USER \"user_name\" CASCADE";
	
	public DBUserTool(Connection connection) throws SQLException {
		this.connection = connection;
		this.statement = this.connection.createStatement();
	}
	
	public void createUser(String username, String password) throws SQLException {
		String createUserSQL = new String(this.createUserSQL);
		createUserSQL = createUserSQL.replace("user_name", username);
		createUserSQL = createUserSQL.replace("password", password);
		
		statement.execute(createUserSQL);
		
	}
	
	public void dropUser(String username) throws SQLException {
		String dropUserSQL = new String(this.dropUserSQL);
		dropUserSQL = dropUserSQL.replace("user_name", username);
		
		statement.execute(dropUserSQL);
		
	}
	
	public void createSuperUser(String username, String password) throws SQLException {
		String createSuperUserSQL = new String(this.createSuperUserSQL);
		createSuperUserSQL = createSuperUserSQL.replace("user_name", username).replace("password", password);
		
		statement.execute(createSuperUserSQL);
	}
}
